package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.framework.component.cache.Cache;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.ucenter.service.UploadService;

@Controller
@RequestMapping("/admin")
public class UploadController {

	private static Logger log = LoggerFactory.getLogger(UploadController.class);

	private static final String IMG_UPLOAD = "/images/upload";
	//XH_IMG_UPLOAD（xheditor上传图片专用）
	private static final String XH_IMG_UPLOAD = "/images/xh/upload";

	@Resource
	private UploadService uploadService;

	@Resource(name="xmemcached")
	private Cache cache;
	
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	/**
	 * 用户头像上传
	 * @param request
	 * @return
	 */
	@RequestMapping(IMG_UPLOAD)
	public JSONObject uploadImage(HttpServletRequest request, HttpServletResponse response) {
		JSONObject result = new JSONObject(); 
		JSONObject responseJson = new JSONObject();

		try {
//			permissionHelper.init(request, response);
        	User user = permissionHelper.getUser();
			response.setCharacterEncoding("utf-8");
			
			responseJson = uploadService.uploadImage((MultipartHttpServletRequest)request, user);
			if(responseJson == null) {
				responseJson.put("code", ApiResultEnum.FAIL.getId());
				responseJson.put("tip", "上传失败");
				return null;
			} 
			
			
		} catch (Exception e) {
			log.error("func[uploadImage] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			responseJson.put("code", ApiResultEnum.FAIL.getId());
			responseJson.put("tip", "上传失败");
			
		}
		return responseJson;
	}
	/**
	 * XHEDITOR上传图片专用
	 * @param request
	 * @return
	 */
	@RequestMapping(XH_IMG_UPLOAD)
	public JSONObject xhUploadImage(HttpServletRequest request, HttpServletResponse response) {
		JSONObject responseJson = new JSONObject();
		try {
//			permissionHelper.init(request, response);
			User user = permissionHelper.getUser();
			response.setCharacterEncoding("utf-8");
			responseJson = uploadService.uploadImage((MultipartHttpServletRequest)request, user);
			Map<String, Object> map = new HashMap<String, Object>();
			if(responseJson == null) {
				responseJson.put("code", ApiResultEnum.FAIL.getId());
				responseJson.put("tip", "上传失败");
				return null;
			} 
			map.put("err", "");
			map.put("msg", responseJson.getJSONObject("response").getJSONObject("result").getJSONArray("imgPaths").getJSONObject(0).get("original"));
			responseJson = new JSONObject(map);
		} catch (Exception e) {
			log.error("func[xhUploadImage] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			responseJson.put("code", ApiResultEnum.FAIL.getId());
			responseJson.put("tip", "上传失败");
		}
		return responseJson;
	}
}
